#!/bin/sh - 
#set -x 

# Use at own RISK  !
# if you want to use an existing packages directory from dragora CD
# mount it and cp -a /mountpoint/packages $pkg_dir (= /tmp/chroot_pkg per default)
# this should probably be added but for now lets keep it simple :) 
# In short: make sure you are happy with $chroot_dir and pkg_dir and pkg_list.


# 1) VARIABLES
# 2) TESTS root and lftp 
# 3) FUNCTIONS
# 4) OPTIONS (c,h,m - create_chroot, help, mount_chroot)


#-----------------------------------
#-----------------------------------
# VARIABLES
#-----------------------------------
chroot_dir="/media/dragora_chroot"
pkg_dir="/tmp/dragora_chroot_pkg"
pkg_count=$(find "$pkg_dir" -name '*tlz' | wc -l)
option="$1" #needed for getopts, as $@ is used for repo_list (posix)
set -- 	'http://gungre.ch/dragora/mirror/v2/packages/base' \
	'http://gungre.ch/dragora/mirror/v2/packages/devel' \
	'http://gungre.ch/dragora/mirror/v2/packages/libraries' \
	'http://gungre.ch/dragora/mirror/v2/packages/modules' \
	'http://gungre.ch/dragora/mirror/v2/packages/networking' \
	'http://gungre.ch/dragora/mirror/v2/packages/tools'

#skipped packages: 
#http://gungre.ch/dragora/mirror/v2/packages/java
#http://gungre.ch/dragora/mirror/v2/packages/kernel
#http://gungre.ch/dragora/mirror/v2/packages/media
#http://gungre.ch/dragora/mirror/v2/packages/printing
#http://gungre.ch/dragora/mirror/v2/packages/scanner
#http://gungre.ch/dragora/mirror/v2/packages/x-applications
#http://gungre.ch/dragora/mirror/v2/packages/x-libraries
#http://gungre.ch/dragora/mirror/v2/packages/x-xfce
#http://gungre.ch/dragora/mirror/v2/packages/x-xfce-goodies
#http://gungre.ch/dragora/mirror/v2/packages/x-xfce-xorg


#----------------------------------
#-----------------------------------
# TESTS
#-----------------------------------
# test root
if [ "$(id -u )" != 0 ]
then 
	printf "\nrun as root\n"
	exit 1
fi

if ! command -v lftp >/dev/null; 
	then printf "dep lftp missing\n"; 
	exit 1;  
fi 


#----------------------------------
#----------------------------------
# FUNCTIONS  
#-----------------------------------
# usage 
usage () {
	printf "%s \n" \
	" "  \
	"	Usage: dragora_chroot [options]" \
	"	" \
	"	-c " \
	"	create dragora chroot at:" \
	"	default chroot_dir: " "	$chroot_dir" \
	"	from " \
	"	default pkg_dir: " "	$pkg_dir" \
	"	" \
	"	- Edit variables to change default" \
	"	- mount iso and cp -a  packages-directory to pkg_dir " \
	"	- $pkg_dir" \
	"	- to avoid downloading from repo" \
	"	 " \
	"	-m " \
	"	mount dev, sys, proc for chroot_dir" \
	"	chroot chroot_dir" \
	"	Edit chroot_dir to change default" "	$chroot_dir" \
	"	" \
	"	-h " \
	"	display help" \
	"	" 
}


#---
# create_chroot
create_chroot() {
if [ -d "$chroot_dir" ]
then
	printf "%s does exist already. \nChoose a different chroot path\n" "$chroot_dir"
	exit 1
else 
	mkdir "$chroot_dir" || exit 1
fi

# create or install from pkg_dir
if [ -d "$pkg_dir" ] && [ "$pkg_count" -ne 0 ]  
then 
	cd "$pkg_dir" || exit 1
	find . -type f -name '*.tlz' | while read -r file ; do  ROOT="$chroot_dir" pkg add "$file"  ; done
else 	
	if ! [ -d "$pkg_dir" ]; then 
	  mkdir "$pkg_dir" || exit 1
	fi 
	cd "$pkg_dir"  || exit 1 
	for i in "$@"
	do
		echo 'mirror' | lftp "$i"  
	done
	find . -type  f -name '*.tlz'| while read -r file; do  ROOT="$chroot_dir" pkg add "$file"  ; done
fi 

# internet access 
if [ -d "$chroot_dir"/etc ]; then
	cp /etc/resolv.conf "$chroot_dir"/etc
fi 
}


#---
# mount_chroot 
mount_chroot() {
	mount -o bind /proc "$chroot_dir"/proc
#	mount --rbind /dev "$chroot_dir"/dev
	mount -o bind /dev "$chroot_dir"/dev
	mount -o bind /sys  "$chroot_dir"/sys 
	chroot "$chroot_dir" 
}



#-----------------------------------
#-----------------------------------
# OPTIONS
#-----------------------------------
# running without argument will do noting, but help
while :;   
do 
	case "$option" in
	-c) 
		create_chroot "$@"
		break
		;;
	-h) 
		usage
	   	exit 0
		;; 
	-m)
		mount_chroot
		break
	   	;;
	*)
		usage
		break
		;;
	--)
		shift
		break
		;;
	esac 
done 


exit 0 
